//
//  HSLeftViewController.swift
//  HomeSchoolDemo
//
//  Created by 张晓林 on 16/9/27.
//  Copyright © 2016年 张晓林. All rights reserved.
//

import UIKit
import SnapKit

class HSLeftViewController: UIViewController {
    /// 左边按钮数组
    @IBOutlet var leftBtnArray: [UIButton]!
    /// 头像
    @IBOutlet weak var leftIconView: UIImageView!
    /// 用户名称
    @IBOutlet weak var leftNameL: UILabel!
    /// 账号
    @IBOutlet weak var leftAccountL: UILabel!
    /// 班级名称
    @IBOutlet weak var leftClassNameL: UILabel!
    /// 学校名称
    @IBOutlet weak var leftSchoolNameL: UILabel!
    /// 表现分
    @IBOutlet weak var leftScoreL: UILabel!
    /// 分数分级视图
    @IBOutlet weak var leftScoreBgView: UIView!
    /// 头像宽度
    @IBOutlet weak var leftIconConstraint: NSLayoutConstraint!
    
    
    // MARK: - 按钮的监听回调方法
    @IBAction func leftBtnClicked(_ sender: UIButton) {
        switch sender.currentTitle! {
        case "课程表":
            present(HSClassTableController(), animated: true, completion: nil)
            
            case "关于我们":
            print("点击了关于我们")
            
            case "意见反馈":
            print("点击了意见反馈")
            
        default:
            // 弹窗确认是否退出
            let alertVc = UIAlertController(title: "确定要退出吗？", message: nil, preferredStyle: .alert)
            // 添加确定按钮
            alertVc.addAction(UIAlertAction(title: "确定", style: .default, handler: { (_) in
                // 获得登录页面控制器
                let loginVc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "HSLoginController")
                UIApplication.shared.keyWindow?.rootViewController = loginVc
                // 修改登录状态
                UserDefaults.standard.set(false, forKey: isLogin)
            }))
            // 取消按钮
            alertVc.addAction(UIAlertAction(title: "取消", style: .cancel, handler: nil))
            present(alertVc, animated: true, completion: nil)
        }
    }
    
    // MAKR: - UI设置
    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
    }
    
    /// 设置UI
    fileprivate func setupUI() {
        // 设置背景
        view.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        // 调整头像大小
        leftIconConstraint.constant = screenW <= 320 ? 60 : 80
        // 设置头像圆角
        leftIconView.layer.cornerRadius = leftIconConstraint.constant/2
        leftIconView.layer.masksToBounds = true
        // 设置边框
        leftIconView.layer.borderWidth = 2
        leftIconView.layer.borderColor = UIColor.white.cgColor
        
        // 设置左边按钮的高亮背景
        for btn in leftBtnArray {
            // 获得背景图片
            let image = UIImage.xx_image(color: #colorLiteral(red: 0.9620869756, green: 0.9586330056, blue: 0.9656943679, alpha: 1), size: btn.bounds.size)
            btn.setBackgroundImage(image, for: .highlighted)
        }
        
        // 获得保存的用户信息数据
        let userInfo = NSDictionary(contentsOfFile: saveUserInfoPath)?["data"] as? NSDictionary
        // 头像
        if let iconStr = userInfo?["studentphoto"] as? String {
            let iconData = Data(base64Encoded: iconStr, options: Data.Base64DecodingOptions.ignoreUnknownCharacters)
            leftIconView.image = UIImage(data: iconData!)
        }
        // 姓名
        leftNameL.text = (userInfo?["studentName"] as? String ?? "") + "    家长"
        leftAccountL.text = userInfo?["studentNum"] as? String
        leftClassNameL.text = userInfo?["classname"] as? String
        leftSchoolNameL.text = userInfo?["schoolname"] as? String
        leftScoreL.text = userInfo?["studentScore"] as? String
        
        // 获得保存的用户信息
        let score = Int(leftScoreL.text ?? "") ?? 0
        // 判断分数比值
        var scoreRank: Int = 0
        switch score {
        case 0..<10:
            scoreRank = 0
        case 10..<50:
            scoreRank = 1
        case 50..<150:
            scoreRank = 2
        case 150..<350:
            scoreRank = 3
        case 350..<850:
            scoreRank = 4
        default:
            scoreRank = 5
        }
        
        // 添加视图
        for i in 1..<6 {
            let imageNmae = scoreRank >= i ? "jujube\(i)_light" : "jujube\(i)_dark"
            let imageV = UIImageView(image: UIImage(named: imageNmae))
            imageV.tag = 99 + i
            leftScoreBgView.addSubview(imageV)
        }
        
    }
    
    // 在视图显示完成后设置分数等级视图位置
    override func viewDidAppear(_ animated: Bool) {
        // 添加视图
        for iv in leftScoreBgView.subviews {
            // 根据背景图的宽度获得图片左边的距离
            let margin = leftScoreBgView.frame.width / 5
            // 计算x中心值
            let x = margin * CGFloat(iv.tag - 100) + iv.bounds.width/2
            // 设置约束
            iv.snp.makeConstraints({ (make) in
                make.centerX.equalTo(leftScoreBgView.snp.left).offset(x)
                make.bottom.equalTo(leftScoreBgView.snp.bottom).offset(-5)
            })
        }
    }

 
}










